﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;
using CMS.Data;
using AjaxPro;

namespace CMS.Presenters
{
    public class AppSettingsPresenter
    {
        IAppSettingsView view;
        IDiaBanDao diabanDao;       

        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database.
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        //Attaches this presenter to the view's events.
        public AppSettingsPresenter(IAppSettingsView view)
        {
            this.view = view;
            SubscribeViewToEvents();
        }
        public AppSettingsPresenter()
        {

        }

        void SubscribeViewToEvents()
        {
            view.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
            if (!view.IsPostBack)
            {
                //LoadViewFromModel();
                //view.DataBind();
            }
        }

        public void LoadViewFromModel(Configuration config)
        {
            this.diabanDao = NHibernateDaoFactory.Instance.GetDiaBanDao();

            IList<DiaBan> dbList = this.diabanDao.GetAllDiaBanCha();//.GetFieldEqualCriteria("TrangThai", true);
            view.DiaBanList = dbList;

            //view.LoginType = CauHinhHeThong.LoginType;
            //view.LDAPServer = CauHinhHeThong.LDAPServer;
            //view.LDAPAttrib = CauHinhHeThong.LDAPAttrib;
            //view.LDAPAttribExt = CauHinhHeThong.LDAPAttribExt;
            //view.LDAPGroup = CauHinhHeThong.LDAPGroup;
            //view.LDAPLogin = CauHinhHeThong.LDAPLogin;
            view.UploadPath = CauHinhHeThong.UploadPath;
            view.Email = CauHinhHeThong.AdminEmail;
            view.EmailPass = CauHinhHeThong.SmtpUserPass;
            //String temp = CauHinhHeThong.MaDiaBan;
            //view.MaDiaBan = temp;

            String[] conParams = CauHinhHeThong.ConnectionParameters;
            view.DataSource = conParams[0];
            view.Database = conParams[1];
            view.UserID = conParams[2];
            view.Password = conParams[3];
        }

       
        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        public void UpdateConfigs(Configuration config)
        {

            
            // Find app setting to change
            //Email
            KeyValueConfigurationElement element = (KeyValueConfigurationElement)config.AppSettings.Settings["AdminEmail"];
            if (element != null)
            {
                element.Value = view.Email;
            }
            element = (KeyValueConfigurationElement)config.AppSettings.Settings["SmtpUserName"];
            if (element != null)
            {
                element.Value = view.Email;
            }
            //Password email
            element = (KeyValueConfigurationElement)config.AppSettings.Settings["SmtpUserPass"];
            if (element != null)
            {
                element.Value = view.EmailPass;
            }
            //UploadPath
            element = (KeyValueConfigurationElement)config.AppSettings.Settings["UploadPath"];
            if (element != null)
            {
                element.Value = view.UploadPath;
            }
            //MaDiaBan
            element = (KeyValueConfigurationElement)config.AppSettings.Settings["MaDiaBan"];
            if (element != null)
            {
                element.Value = view.MaDiaBan;
            }
            //TenDiaBan
            element = (KeyValueConfigurationElement)config.AppSettings.Settings["TenDiaBan"];
            if (element != null)
            {
                element.Value = view.TenDiaBan;
            }
            ////LoginType
            //element = (KeyValueConfigurationElement)config.AppSettings.Settings["LoginType"];
            //if (element != null)
            //{
            //    element.Value = view.LoginType;
            //}
            ////LDAPServer
            //element = (KeyValueConfigurationElement)config.AppSettings.Settings["LDAPServer"];
            //if (element != null)
            //{
            //    element.Value = view.LDAPServer;
            //}
            ////LDAPLogin
            //element = (KeyValueConfigurationElement)config.AppSettings.Settings["LDAPLogin"];
            //if (element != null)
            //{
            //    element.Value = view.LDAPLogin;
            //}
            ////LDAPAttrib
            //element = (KeyValueConfigurationElement)config.AppSettings.Settings["LDAPAttrib"];
            //if (element != null)
            //{
            //    element.Value = view.LDAPAttrib;
            //}
            ////LDAPAttribExt
            //element = (KeyValueConfigurationElement)config.AppSettings.Settings["LDAPAttribExt"];
            //if (element != null)
            //{
            //    element.Value = view.LDAPAttribExt;
            //}
            ////LDAPGroup
            //element = (KeyValueConfigurationElement)config.AppSettings.Settings["LDAPGroup"];
            //if (element != null)
            //{
            //    element.Value = view.LDAPGroup;
            //}
            //ConnectionString
            element = (KeyValueConfigurationElement)config.AppSettings.Settings["ConnectionString"];
            if (element != null)
            {

                element.Value = "Data Source=" + view.DataSource + ";Database=" + view.Database + ";User ID=" + view.UserID + ";Password=" + view.Password + ";";
            }
            
            try
            {
                config.Save();
                view.IsFinishUpdate = true;

                UpdateCompleteEvent(this, null);
            }
            catch (Exception ex)
            {
                view.Msg = "ASP.NET process account (bao gồm local ASPNET hoặc Network Service account) "
                    + "phải được cấp quyền write cho file Web.config!";
            }
        }
        
        [AjaxMethod]
        public bool CheckExists(String uploadFolder)
        {
            return System.IO.Directory.Exists(uploadFolder);
        }
    }
}
